package nc.ui.med.pub.query;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.JComponent;

import nc.ui.pub.beans.UIRefPane;
import nc.ui.querytemplate.QueryConditionEditorContext;
import nc.ui.querytemplate.filter.IFilter;
import nc.ui.querytemplate.meta.FilterMeta;
import nc.ui.querytemplate.value.IFieldValueElement;
import nc.ui.querytemplate.valueeditor.IFieldValueEditor;
import nc.ui.querytemplate.valueeditor.RefElementEditor;
import nc.vo.med.pub.MedEnv;
import nc.vo.pub.query.IQueryConstants;

@SuppressWarnings( { "unchecked", "serial", "deprecation", "unused",
		"static-access" })
public class BatchFilterEditor extends MedDefaultFilterEditor {

	public static class BatchFilter extends MedDefaultFilter {

		private String cinvmandidfield;

		private String cinvmanidvalue;

		public String getCinvidField() {
			return cinvmandidfield;
		}

		public void setCinvidField(String invmandidField) {
			this.cinvmandidfield = invmandidField;
		}

		public BatchFilter(FilterMeta field, QueryConditionBaseDLG dlg,
				String invmandidfeild, String invmanidvalue) {
			super(field, dlg);
			this.cinvmandidfield = invmandidfeild;
			this.cinvmanidvalue = invmanidvalue;
		}

		@Override
		public String getSqlString() {

			String swhere = super.getSqlString();
			String stemp = null;
			if (getSqlprocess() != null)
				stemp = getSqlprocess().processWhere(this, swhere);
			if (stemp != null && stemp.trim().length() > 0)
				return stemp;
			return swhere;
		}

		@Override
		public String toString() {
			return super.toString();
		}

		public String getCinvmanidvalue() {
			return cinvmanidvalue;
		}

		public void setCinvmanidvalue(String invidvalue) {
			this.cinvmanidvalue = invidvalue;
		}

	}

	private String invidField;

	private UIRefPane batchItemRefPane;

	@Override
	protected IFieldValueEditor createFieldValueEditor() {
		RefElementEditor re = new RefElementEditor(getBatchItemRefPane(),
				IQueryConstants.RETURNCODE) {

			public List<IFieldValueElement> getValues() {
				super.setFuzzyEditing(true);
				return super.getValues();
			}
		};
		re.setFuzzyEditing(true);
		IFieldValueEditor ret = new MedDefaultFieldValueEditor(re, re);
		ret.addActionListener(this);
		return ret;
	}

	public BatchFilterEditor(QueryConditionEditorContext context,
			FilterMeta meta, QueryConditionBaseDLG dlg, String invmanidfield) {
		super(context, meta, dlg);
		this.invidField = invmanidfield;
		initialize();
	}

	public BatchFilterEditor(QueryConditionEditorContext context,
			IFilter filter, QueryConditionBaseDLG dlg, String invmanidfield) {
		super(context, filter, dlg);
		this.invidField = invmanidfield;
		initialize();
	}

	private void initialize() {
	}

	public String getInvID() {
		try {
			return (String) getBatchItemRefPane().getClass().getMethod(
					"getCinventoryid", null)
					.invoke(getBatchItemRefPane(), null);
		} catch (Throwable e) {
			MedEnv.error(e);
		}
		return null;
	}

	@Override
	protected IFilter createFilter(FilterMeta meta) {
		return new BatchFilter(meta, getQueryDlg(), getInvManIdField(),
				getInvID());
	}

	@Override
	public IFilter getFilter() {
		IFilter filter = super.getFilter();
		if (filter == null)
			return filter;
		if (filter instanceof BatchFilter) {
			((BatchFilter) filter).setCinvidField(this.getInvManIdField());
			((BatchFilter) filter).setCinvmanidvalue(this.getInvID());
		} else {
			filter = new BatchFilter((FilterMeta) filter.getFilterMeta(),
					getQueryDlg(), this.getInvManIdField(), this.getInvID());
		}
		return filter;
	}

	@Override
	public void setFilter(IFilter filter) {
		super.setFilter(filter);

	}

	public String getInvManIdField() {
		return invidField;
	}

	public void setInvFieldCode(String invmanidField) {
		this.invidField = invmanidField;
	}

	public UIRefPane getBatchItemRefPane() {
		if (this.batchItemRefPane == null) {
			try {
				this.batchItemRefPane = (UIRefPane) Class.forName(
						"nc.ui.gsp.pub.lotquery.LotQueryRef").getConstructor(
						new Class[] { Container.class }).newInstance(
						new Object[] { this });
				this.batchItemRefPane.setAutoCheck(false);
			} catch (Throwable e) {
				MedEnv.error(e);
			}
		}
		return batchItemRefPane;
	}

	@Override
	public JComponent getFilterEditorComponent() {
		return super.getFilterEditorComponent();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		getBatchItemRefPane().getRefModel().clearData();
		super.actionPerformed(e);
	}

}
